package ru.picman;

import android.util.Log;
import android.view.KeyEvent;

public class Player
             implements KeyListener
{
	public Player(String name, Unit unit) {
		this.name = name;
		this.unit = unit;
		setLives(3);
		setScore(0);
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	
	public Unit getUnit() {
		return unit;
	}
	
	/**
	 * The Bird has died. One live will be lost and if there
	 * are no lives left the bird will be removed from the game
	 * and {@link PicmanGame#gameOver} will be called.
	 */
	public void die() {
		--lives;
		if (lives <= 0) {
			getUnit().getWorld().removeEntity(getUnit());
			getUnit().getWorld().getGame().gameOver();
		}
	}
	
	/**
	 * Sets the amount of lives of this Bird to <i>lives</i>.
	 * 
	 * @param lives The desired amount of lives
	 */
	public void setLives(int lives) {
		if (lives <= 0)
			throw new IllegalArgumentException("Lives should be > 0");
		this.lives = lives;
	}
	
	/**
	 * Returns the amount of lives left.
	 * 
	 * @return The lives
	 */
	public int getLives() {
		return lives;
	}
	
	/**
	 * Sets the player score
	 * 
	 * @param score The score
	 */
	public void setScore(int score) {
		this.score = score;
	}
	
	/**
	 * Increases the score by one.
	 */
	public void incScore() {
		++score;
	}
	
	/**
	 * Returns the player score
	 * 
	 * @return The score
	 */
	public int getScore() {
		return score;
	}
	
	
	public void onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_UP:
				unit.setDesiredDirection(Unit.DIRECTION_UP);
				break;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				unit.setDesiredDirection(Unit.DIRECTION_DOWN);
				break;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				unit.setDesiredDirection(Unit.DIRECTION_LEFT);
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				unit.setDesiredDirection(Unit.DIRECTION_RIGHT);
				break;
			case KeyEvent.KEYCODE_DPAD_CENTER:
				unit.setDesiredDirection(Unit.DIRECTION_NONE);
				break;
			default:
				Log.v("KeyEvent","Code: " + keyCode);
				break;
		}
	}
	
	//
	
	private String name;
	private Unit   unit;
	
	private int    score;
	private int    lives;
}
